package my.app.timer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Toast;

public class PlayerFragment extends Fragment 
{

	private RelativeLayout rl;
	private ImageButton pause;
	private SeekBar seekBar;
	private ListView lv;
	private ArrayList<SongEntry> songList;
	private MediaPlayer mediaPlayer;
	private boolean mpPauseFlag;
	private int pos;
	private int length = 0;
	private View myView;
	public PlayerFragment() 
	{
		
	}


	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		mediaPlayer = new MediaPlayer();
		mpPauseFlag = true;
		
		try {
			playlistinit();
		} 
        catch (FileNotFoundException e) {e.printStackTrace();}		
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) 
	{
		View rootView = inflater.inflate(R.layout.player_layout,container, false);
		myView = rootView;
		rl = (RelativeLayout)rootView.findViewById(R.id.relLayout);
		rl.setBackgroundColor(Color.GRAY);

		seekBar = (SeekBar) rootView.findViewById(R.id.seekBar1);
        lv = (ListView) rootView.findViewById(R.id.listView);
        pause = (ImageButton) rootView.findViewById(R.id.imageButton);                
        new SeekBarUpdate(mediaPlayer, seekBar);
    
        lv.setAdapter(new SongListAdapter(rootView.getContext(), songList)
        {
        	
        }); 
        
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() 
        {			
			@Override
			public void onCompletion(MediaPlayer mp) 
			{				
				//seeking.pause();
				mp.reset();
				pos++;
				
				try {
					while(songList.get(pos).songPath==null)
						pos++;		
					mp.setDataSource(songList.get(pos).songPath);
					
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				try {
					mediaPlayer.prepare();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				mediaPlayer.start();
				//seeking.resume();	
			}
		});
        
        pause.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				pauseORresume();
			}
		});
        lv.setOnItemClickListener(new OnItemClickListener()
        {   	
			public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) 
			{
//				view.animate().alphaBy(30);
//				view.setBackgroundColor(Color.BLUE);
				
				try 
				{
					if(mediaPlayer.isPlaying() || mpPauseFlag==false)
						mediaPlayer.reset();
					pos=position;
					while(songList.get(pos).songPath==null)
						pos++;	
					mediaPlayer.setDataSource(songList.get(pos).songPath);
					
				}
				catch (IllegalArgumentException e)	{e.printStackTrace();} 
				catch (SecurityException e)			{e.printStackTrace();}
				catch (IllegalStateException e) 	{e.printStackTrace();}
				catch (IOException e) 				{e.printStackTrace();}				
				
				try
				{
					mediaPlayer.prepare();
				} 
				catch (IllegalStateException e) {e.printStackTrace();} 
				catch (IOException e) 			{e.printStackTrace();}
				
				
				mediaPlayer.start();
			}
        	
		});
        
		return rootView;
	}
	
	private void playlistinit() throws FileNotFoundException 
    {
    	songList = new ArrayList<SongEntry>();
    	//File dir = new File("/storage/sdcard0/music");
    	File dir = new File("/storage/emulated/0/Download");
    	FileWalker fl = new FileWalker();
    	fl.walk(dir, songList);
	}
	
	public void pauseORresume()
    {
		Context context = myView.getContext();
    	if(mediaPlayer.isPlaying())//pause
    	{
    		
    	    mediaPlayer.pause();
    	    length = mediaPlayer.getCurrentPosition();
    	    
    	    Toast.makeText(context,
    	    		"paused", Toast.LENGTH_SHORT).show();
    	    mpPauseFlag = false;
    	    
    	} 
    	else
    		if(length>0)//resume
    	{	
    		mediaPlayer.seekTo(length);
    	    mediaPlayer.start();
    	    
    	    Toast.makeText(context,
    	    		"playing", Toast.LENGTH_SHORT).show();
    	    mpPauseFlag = true;
    	}
    }
}